package Util::TuanZhang::TuanZhang;
use Moose;
use namespace::autoclean;
use JSON;
use Time::Piece;
use Image::Magick;
use File::Basename;

has 'schema' => (
	is => 'rw',
	required => 1,
	isa => 'DBIx::Class::Schema',
);

#####################################################
#Input: 
#	user_id	
#Return: 
#	requests		
#####################################################
sub find {
	my ($self, $request_id) = @_;

	return  $self->schema->resultset('TuanZhang')->search({'id' => $request_id},{})->first();
}

sub tuanzhang_request_search {
	my ($self, $user_id) = @_;

	return  $self->schema->resultset('TuanZhang')->search({'user_id' => $user_id},{})->first();
}

sub find_by_status {
	my ($self, $status) = @_;

	return  $self->schema->resultset('TuanZhang')->search({'status' => $status},{});
}

sub find_all {
	my ($self, $user_id) = @_;

	return  $self->schema->resultset('TuanZhang')->search({},{order_by => {-desc => [qw/updated/]}});
}

sub find_all_count {
	my ($self, $user_id) = @_;

	return  $self->schema->resultset('TuanZhang')->search({},{})->count();
}


sub update_status {
	my ($self, $request_id, $status, $user_id) = @_;
	my $request = $self->schema->resultset('TuanZhang')->search({id => $request_id }, {})->first();
	$request->update({
		status => $status,
		updated_by => $user_id,
	});
	return $request;
}

sub reject_renzheng {
	my ($self, $request_id, $status, $user_id, $comment) = @_;
	my $request = $self->schema->resultset('TuanZhang')->search({id => $request_id }, {})->first();
	$request->update({
		status => $status,
		shenpi_comment => $comment,
		updated_by => $user_id,
	});
	return $request;
}

sub create {
	my ($self, $user_id, $actual_name, $idno, $shouji, $dizhi, $is_student, $xuehao, $xibie, $nianji, $shenfengzheng, $xueshengzheng, $neirong, $university_id,$static) = @_;

	my ($t_filename, $tmp_target, $small_image, $big_image, $f_suffix);

        if($shenfengzheng){
                my($filename, $directories, $suffix) = fileparse($shenfengzheng->filename, qr/\.[^.]*/);
                $f_suffix = $suffix;
                my $rand = new String::Random;
                $t_filename = $rand->randpattern("CCCCCcccccnnnnncccccCCCCC");

                my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
                $year = 1900 + $year;
		$mon = 1 + $mon;
                my $dir = "/$year/$mon/$mday";

                $t_filename = $t_filename . $suffix;
                $tmp_target = "/tmp/$dir/$t_filename";

                system("mkdir -p \"/tmp/$dir\"") unless(-e "/tmp/$dir");
                system("mkdir -p \"$static/small/$dir\"") unless(-e "$static/small/$dir");
                system("mkdir -p \"$static/big/$dir\"") unless(-e "$static/big/$dir");
                $small_image = "$static/small/$dir/$t_filename";
                $big_image = "$static/big/$dir/$t_filename";
                $t_filename = "$dir/$t_filename";

                unless ( $shenfengzheng->link_to($tmp_target) || $shenfengzheng->copy_to($tmp_target) ) {
                        die( "Failed to copy '$filename' to '$tmp_target': $!" );
                }

                $f_suffix =~ s/\.//;
		my $t = new Image::Magick::Thumbnail::Fixed;
                $t->thumbnail( input   => $tmp_target,
                                output  => $small_image,
                                width   => 279,
                                height  => 212,
                                density => '279x212',
                                gravity => 'center',
                                compose => 'over',
                                quality => '90',
                                bgcolor => 'white',
                                format  => $f_suffix );

                $t->thumbnail( input   => $tmp_target,
                                output  => $big_image,
                                width   => 540,
                                height  => 411,
                                density => '540x411',
                                gravity => 'center',
                                compose => 'over',
				quality => '90',
                                bgcolor => 'white',
                                format  => $f_suffix );

        } 
	#else {
        #        $t_filename = "tuan/default.jpg";
        #}

	my ($t_filename1, $tmp_target1, $small_image1, $big_image1, $f_suffix1);

        if($xueshengzheng){
                my($filename1, $directories1, $suffix1) = fileparse($xueshengzheng->filename, qr/\.[^.]*/);
                $f_suffix1 = $suffix1;
                my $rand1 = new String::Random;
                $t_filename1 = $rand1->randpattern("CCCCCcccccnnnnncccccCCCCC");

                my ($sec1,$min1,$hour1,$mday1,$mon1,$year1,$wday1,$yday1,$isdst1) = localtime(time);
                $year1 = 1900 + $year1;
		$mon1 = 1 + $mon1;
                my $dir1 = "/$year1/$mon1/$mday1";

                $t_filename1 = $t_filename1 . $suffix1;
                $tmp_target1 = "/tmp/$dir1/$t_filename1";

                system("mkdir -p \"/tmp/$dir1\"") unless(-e "/tmp/$dir1");
                system("mkdir -p \"$static/small/$dir1\"") unless(-e "$static/small/$dir1");
                system("mkdir -p \"$static/big/$dir1\"") unless(-e "$static/big/$dir1");
                $small_image1 = "$static/small/$dir1/$t_filename1";
                $big_image1 = "$static/big/$dir1/$t_filename1";
                $t_filename1 = "$dir1/$t_filename1";

                unless ( $xueshengzheng->link_to($tmp_target1) || $xueshengzheng->copy_to($tmp_target1) ) {
                        die( "Failed to copy '$filename1' to '$tmp_target1': $!" );
                }

                $f_suffix1 =~ s/\.//;
		my $t1 = new Image::Magick::Thumbnail::Fixed;
                $t1->thumbnail( input   => $tmp_target1,
                                output  => $small_image1,
                                width   => 279,
                                height  => 212,
                                density => '279x212',
                                gravity => 'center',
                                compose => 'over',
                                quality => '90',
                                bgcolor => 'white',
                                format  => $f_suffix1 );

                $t1->thumbnail( input   => $tmp_target1,
                                output  => $big_image1,
                                width   => 540,
                                height  => 411,
                                density => '540x411',
                                gravity => 'center',
                                compose => 'over',
				quality => '90',
                                bgcolor => 'white',
                                format  => $f_suffix1 );

        } 
	#else {
        #        $t_filename1 = "tuan/default.jpg";
        #}
	
	my $request = $self->schema->resultset('TuanZhang')->create({
			user_id => $user_id,
			actual_name => $actual_name,
			address => $dizhi,
			id_number  => $idno,
			#默认为身份证
			id_type => 1,
			id_photo => $t_filename,
			cell_phone => $shouji,
			is_student => $is_student,
			student_no => $xuehao,
			student_card_photo => $t_filename1,
			university_id => $university_id,
			department => $xibie,
			grade => $nianji,
			status => 'R',
			req_detail => $neirong,
			created_by => $user_id,
			updated_by => $user_id
			});
	return $request;
}

sub update {
	my ($self,$tz_id, $user_id, $actual_name, $idno, $shouji, $dizhi, $is_student, $xuehao, $xibie, $nianji, $shenfengzheng, $xueshengzheng, $neirong, $university_id,$static) = @_;
	
	my $tuanzhang_request =  $self->schema->resultset('TuanZhang')->search({'id' => $tz_id},{})->first();

	my ($t_filename, $tmp_target, $small_image, $big_image, $f_suffix);

        if($shenfengzheng){
                my($filename, $directories, $suffix) = fileparse($shenfengzheng->filename, qr/\.[^.]*/);
                $f_suffix = $suffix;
                my $rand = new String::Random;
                $t_filename = $rand->randpattern("CCCCCcccccnnnnncccccCCCCC");

                my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
                $year = 1900 + $year;
		$mon = 1 + $mon;
                my $dir = "/$year/$mon/$mday";

                $t_filename = $t_filename . $suffix;
                $tmp_target = "/tmp/$dir/$t_filename";

                system("mkdir -p \"/tmp/$dir\"") unless(-e "/tmp/$dir");
                system("mkdir -p \"$static/small/$dir\"") unless(-e "$static/small/$dir");
                system("mkdir -p \"$static/big/$dir\"") unless(-e "$static/big/$dir");
                $small_image = "$static/small/$dir/$t_filename";
                $big_image = "$static/big/$dir/$t_filename";
                $t_filename = "$dir/$t_filename";

                unless ( $shenfengzheng->link_to($tmp_target) || $shenfengzheng->copy_to($tmp_target) ) {
                        die( "Failed to copy '$filename' to '$tmp_target': $!" );
                }

                $f_suffix =~ s/\.//;
		my $t = new Image::Magick::Thumbnail::Fixed;
                $t->thumbnail( input   => $tmp_target,
                                output  => $small_image,
                                width   => 279,
                                height  => 212,
                                density => '279x212',
                                gravity => 'center',
                                compose => 'over',
                                quality => '90',
                                bgcolor => 'white',
                                format  => $f_suffix );

                $t->thumbnail( input   => $tmp_target,
                                output  => $big_image,
                                width   => 540,
                                height  => 411,
                                density => '540x411',
                                gravity => 'center',
                                compose => 'over',
				quality => '90',
                                bgcolor => 'white',
                                format  => $f_suffix );

        } else {
                $t_filename = $tuanzhang_request->id_photo;
        }

	my ($t_filename1, $tmp_target1, $small_image1, $big_image1, $f_suffix1);

        if($xueshengzheng){
                my($filename1, $directories1, $suffix1) = fileparse($xueshengzheng->filename, qr/\.[^.]*/);
                $f_suffix1 = $suffix1;
                my $rand1 = new String::Random;
                $t_filename1 = $rand1->randpattern("CCCCCcccccnnnnncccccCCCCC");

                my ($sec1,$min1,$hour1,$mday1,$mon1,$year1,$wday1,$yday1,$isdst1) = localtime(time);
                $year1 = 1900 + $year1;
		$mon1 = 1 + $mon1;
                my $dir1 = "/$year1/$mon1/$mday1";

                $t_filename1 = $t_filename1 . $suffix1;
                $tmp_target1 = "/tmp/$dir1/$t_filename1";

                system("mkdir -p \"/tmp/$dir1\"") unless(-e "/tmp/$dir1");
                system("mkdir -p \"$static/small/$dir1\"") unless(-e "$static/small/$dir1");
                system("mkdir -p \"$static/big/$dir1\"") unless(-e "$static/big/$dir1");
                $small_image1 = "$static/small/$dir1/$t_filename1";
                $big_image1 = "$static/big/$dir1/$t_filename1";
                $t_filename1 = "$dir1/$t_filename1";

                unless ( $xueshengzheng->link_to($tmp_target1) || $xueshengzheng->copy_to($tmp_target1) ) {
                        die( "Failed to copy '$filename1' to '$tmp_target1': $!" );
                }

                $f_suffix1 =~ s/\.//;
		my $t1 = new Image::Magick::Thumbnail::Fixed;
                $t1->thumbnail( input   => $tmp_target1,
                                output  => $small_image1,
                                width   => 279,
                                height  => 212,
                                density => '279x212',
                                gravity => 'center',
                                compose => 'over',
                                quality => '90',
                                bgcolor => 'white',
                                format  => $f_suffix1 );

                $t1->thumbnail( input   => $tmp_target1,
                                output  => $big_image1,
                                width   => 540,
                                height  => 411,
                                density => '540x411',
                                gravity => 'center',
                                compose => 'over',
				quality => '90',
                                bgcolor => 'white',
                                format  => $f_suffix1 );

        } else {
                $t_filename1 = $tuanzhang_request->student_card_photo;
        }
	
	
	$tuanzhang_request->update({
			user_id => $user_id,
			actual_name => $actual_name,
			address => $dizhi,
			id_number  => $idno,
			#默认为身份证
			id_type => 1,
			id_photo => $t_filename,
			cell_phone => $shouji,
			is_student => $is_student,
			student_no => $xuehao,
			student_card_photo => $t_filename1,
			university_id => $university_id,
			department => $xibie,
			grade => $nianji,
			status => 'R',
			req_detail => $neirong,
			updated_by => $user_id
			});
	return $tuanzhang_request;
}


__PACKAGE__->meta->make_immutable;

1;
